package com.atguigu.flink.chapter11;

import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

import java.time.Duration;

import static org.apache.flink.table.api.Expressions.$;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2022/1/22 13:50
 */
public class Flink11_Window__Over_SQL {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        DataStream<WaterSensor> waterSensorStream =
            env.fromElements(new WaterSensor("sensor_1", 1022L, 10),
                             new WaterSensor("sensor_1", 2000L, 20),
                             new WaterSensor("sensor_1", 2000L, 40),
                             new WaterSensor("sensor_1", 5000L, 50),
                             new WaterSensor("sensor_2", 7000L, 30),
                             new WaterSensor("sensor_2", 8000L, 60))
                .assignTimestampsAndWatermarks(
                    WatermarkStrategy
                        .<WaterSensor>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner((ws, ts) -> ws.getTs())
                );
        Table table = tEnv.fromDataStream(waterSensorStream, $("id"), $("ts").rowtime(), $("vc"));
        tEnv.createTemporaryView("sensor", table);
        // 普通的over(不是在计算topN) order by字段只能是时间属性, 而且必须是升序
        
        /*tEnv
            .sqlQuery("select " +
                          " id, " +
                          " ts," +
                          " vc, " +
                          //                          " sum(vc) over(partition by id order by ts rows between unbounded preceding and current row) sum_vc " +
                          //                          " sum(vc) over(partition by id order by ts rows between 1 preceding and current row) sum_vc " +
                          //                          " sum(vc) over(partition by id order by ts range between unbounded preceding and current row) sum_vc " +
                          //                          " sum(vc) over(partition by id order by ts range between interval '2' second preceding and current row) sum_vc " +
                          " sum(vc) over(partition by id order by ts) sum_vc " +  // 省略范围:range between unbounded preceding and current row
                          "from sensor")
            .execute()
            .print();*/
        
        tEnv
            .sqlQuery("select " +
                          " id, " +
                          " ts," +
                          " vc, " +
                          " max(vc) over w max_vc, " +
                          " sum(vc) over w max_vc " +
                          "from sensor " +
                          "window w as(partition by id order by ts rows between unbounded preceding and current row)")
            .execute()
            .print();
        
    }
}
